package com.cngphone.AnFetion2;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.IBinder;

import com.cngphone.AnFetion.FetionListFriends;
import com.cngphone.AnFetion.FetionLoginActivity;
import com.cngphone.AnFetion.FetionService;
import com.cngphone.AnFetion.FrameWork;
import com.cngphone.AnFetion.MiscUtil;
import com.cngphone.AnFetion.Fetion.StatusDefine;


public class AnFetion extends Activity {
    private FetionService mBoundService;

    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName className, IBinder service) {
            mBoundService = ((FetionService.LocalBinder) service).getService();
            bindedInit();
        }

        @Override
        public void onServiceDisconnected(ComponentName className) {
            mBoundService = null;
        }
    };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        // 启动服务
        this.startService(MiscUtil.getFetionServiceIntent(this));
        // 绑定服务
        this.bindService(MiscUtil.getFetionServiceIntent(this), mConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    protected void bindedInit() {
        if (mBoundService.getCurrentStatus() < 0) FrameWork.synchronizedwait(mBoundService, 0);
        showActivity();
        return;
    }

    private void showActivity() {
        Intent intent = null;
        switch (mBoundService.getCurrentStatus()) {
            case StatusDefine.GOTFRIENDS:
                intent = new Intent(AnFetion.this, FetionListFriends.class);
                startActivityIfNeeded(intent, 0);
                finish();
                break;
            case StatusDefine.EMPTY:
                intent = new Intent(AnFetion.this, FetionLoginActivity.class);
                startActivityIfNeeded(intent, 0);
                finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        unbindService(mConnection);
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        MiscUtil.cancelNotification(this, MiscUtil.DIS_CONNECT_NOTIFY_ID);
        super.onResume();
    }
    
}
